package org.thukari.controller;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import org.thukari.entity.DealCountData;
import org.thukari.entity.DealTypeData;
import org.thukari.entity.DealData;
import org.thukari.service.DealWriteOutService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public class DealDataListener implements ReadListener<DealData> {
    private static final DealWriteOutService writeOutService = new DealWriteOutService();

    private static final int BATCH_COUNT = 100;  //一次性最多将多少行存入内存中

    private List<DealData> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    private Map<String, DealCountData> concreteMap = new HashMap<>();

    private static final String REGEX1 = ".*其它+.*";

    private static final String REGEX2 = ".*其他+.*";

    @Override
    public void invoke(DealData dealData, AnalysisContext analysisContext) {
        cachedDataList.add(dealData);
        if (cachedDataList.size() >= BATCH_COUNT) {
            dataProcess(cachedDataList);
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        dataProcess(cachedDataList);
        writeOutService.writeOut(concreteMap);
    }

    private void dataProcess(List<DealData> cachedDataList) {
        for (DealData dealData : cachedDataList) {
            if ("问题单".equals(dealData.getDistrict())||dealData.getDistrict()==null){
                //TODO 等着改
                continue;
            }
            if (Pattern.matches(REGEX1, dealData.getDistrict())) {
                dealData.setDistrict("其它");
            }
            if (Pattern.matches(REGEX2, dealData.getDistrict())) {
                dealData.setDistrict("其它");
            }
            if (concreteMap.get(dealData.getDistrict()) == null) {
                concreteMap.put(dealData.getDistrict(), new DealCountData(dealData.getDistrict(), 1, dealData.getPrice()));
            } else {
                concreteMap.get(dealData.getDistrict()).countUp();
                concreteMap.get(dealData.getDistrict()).moneyUp(dealData.getPrice());
            }
            Map<String,DealCountData> districtMap = concreteMap.get(dealData.getDistrict()).getDataMap();
            if (districtMap.get(dealData.getDealType())==null){
                districtMap.put(dealData.getDealType(),new DealCountData(dealData.getDealType(),1,dealData.getPrice()));
            }else {
                districtMap.get(dealData.getDealType()).countUp();
                districtMap.get(dealData.getDealType()).moneyUp(dealData.getPrice());
            }
            if (dealData.getDealType().equals("01新装")){
                Map<String,DealCountData> typeMap = districtMap.get(dealData.getDealType()).getDataMap();

                if (typeMap.get(dealData.getSetType())==null){
                    typeMap.put(dealData.getSetType(),new DealCountData(dealData.getSetType(),1,dealData.getPrice()));
                }else {
                    typeMap.get(dealData.getSetType()).countUp();
                    typeMap.get(dealData.getSetType()).moneyUp(dealData.getPrice());
                }
            }
        }
    }
}
